package cn.edu.sgu.www.mhxysy.redis.repository.impl;

import cn.edu.sgu.www.mhxysy.consts.RedisKeyPrefixes;
import cn.edu.sgu.www.mhxysy.easyui.component.impl.Tree;
import cn.edu.sgu.www.mhxysy.vo.treegrid.PropCategoryTreeGrid;
import cn.edu.sgu.www.mhxysy.entity.prop.PropCategory;
import cn.edu.sgu.www.mhxysy.redis.RedisUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class PropCategoryRepository {

    private final RedisUtils redisUtils;

    @Autowired
    public PropCategoryRepository(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    public void cacheTree(List<Tree<PropCategory>> list) {
        String key = RedisKeyPrefixes.PREFIX_PROP_TREE;

        redisUtils.set(key, JSON.toJSONString(list));
    }

    public List<Tree<PropCategory>> getCacheOfTree() {
        String key = RedisKeyPrefixes.PREFIX_PROP_TREE;

        if (redisUtils.hasKey(key)) {
            String value = redisUtils.get(key);

            return JSON.parseObject(value, List.class);
        }

        return null;
    }

    public void cacheTreeGrid(List<PropCategoryTreeGrid> list) {
        String key = RedisKeyPrefixes.PREFIX_PROP_TREE_GRID;

        redisUtils.set(key, JSON.toJSONString(list));
    }

    public List<PropCategoryTreeGrid> getCacheOfTreeGrid() {
        String key = RedisKeyPrefixes.PREFIX_PROP_TREE_GRID;


        if (redisUtils.hasKey(key)) {
            String value = redisUtils.get(key);

            return JSON.parseObject(value, List.class);
        }

        return null;
    }

    public void cacheComboTreeGrid(List<PropCategoryTreeGrid> list) {
        String key = RedisKeyPrefixes.PREFIX_PROP_COMBO_TREE_GRID;

        redisUtils.set(key, JSON.toJSONString(list));
    }

    public List<PropCategoryTreeGrid> getCacheOfComboTreeGrid() {
        String key = RedisKeyPrefixes.PREFIX_PROP_COMBO_TREE_GRID;


        if (redisUtils.hasKey(key)) {
            String value = redisUtils.get(key);

            return JSON.parseObject(value, List.class);
        }

        return null;
    }

    public void deleteCache() {
        redisUtils.delete(RedisKeyPrefixes.PREFIX_PROP_TREE);
        redisUtils.delete(RedisKeyPrefixes.PREFIX_PROP_TREE_GRID);
        redisUtils.delete(RedisKeyPrefixes.PREFIX_PROP_COMBO_TREE_GRID);
    }

}